﻿using LanDBom.Models;
using LanDBom.Models.Procure;
using LanDBom.Models.Entity;
using Newtonsoft.Json;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NPOI.SS.Formula.Functions;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using Humanizer;
using LanDBom.Models.price_New_2;
using System.Data.Entity;
using iTextSharp.text;
using System.Text.RegularExpressions;
using FastReport.Barcode;
using MathNet.Numerics.LinearAlgebra.Factorization;
using Microsoft.Office.Interop.Excel;
using Microsoft.Ajax.Utilities;
using System.Data;
using System.Text;
using System.Security;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Kingdee.BOS;
using OfficeOpenXml;
using System.Security.Cryptography;
using System.Windows.Documents;
using System.Collections;
using static System.Windows.Forms.AxHost;
using NPOI.Util;
using System.Web.Helpers;
using Org.BouncyCastle.Asn1.Pkcs;
using System.Windows.Forms;

namespace LanDBom.Controllers
{
    public class ProcureController : Controller
    {
        LDbaseEntities db = new LDbaseEntities();
        Find_APP_information find_APP_Information = new Find_APP_information();
        Find_All_GYS all_GYS = new Find_All_GYS();
        Find_GYS_with_LH GYS_LH = new Find_GYS_with_LH();
        Find_BOM_with_APBian BwAbian = new Find_BOM_with_APBian();
        Find_BOMini FB = new Find_BOMini();
        Find_WL FW = new Find_WL();
        Find_INF_with_App_bian FIWA = new Find_INF_with_App_bian();
        Modlfy_Inf_huiP MIH = new Modlfy_Inf_huiP();
        Find_INF_with_BOMid FIWB = new Find_INF_with_BOMid();
        Find_ZBXX FZ = new Find_ZBXX();
        Add_Ndardo AN = new Add_Ndardo();
        Find_Nd FN = new Find_Nd();
        Modlfy_App_bu MAB = new Modlfy_App_bu();
        Modlfy_Nda MN = new Modlfy_Nda();
        Modlfy_Inf_zhuang MIZ = new Modlfy_Inf_zhuang();
        Modlfy_Nda_shen MNS = new Modlfy_Nda_shen();
        // GET: Procure
        public ActionResult Index(string CG_INFSQD, string App_bu)
        {
            Applications app = db.Applications.Where(s => s.AP_bian == CG_INFSQD).FirstOrDefault();
            app.App_bu = App_bu;
            int shu = db.SaveChanges();
            string message = shu > 0 ? "修改完成" : "未修改";
            return Content(message, "Procure/Overview");
        }

        public ActionResult Valuation_statement(int state = 0, string BB_SQ_xmmc = null, string BB_SQ_khmc = null, string BB_SQ_sqrq = null, string BB_SQ_gjz = null, string jssj = null, string qssj = null, string danhao = null, string xmlx = null, int id = 0)
        {
            if (state == 1)
            {
                var list = db.Applications.Where(s => s.App_bu != null || s.App_zhuang != "8").ToList();
                List<Informa> inf = new List<Informa>();
                foreach (Applications app in list)
                {
                    var informa = db.Informa.Where(x => x.Inf_shen == app.AP_bian).ToList();
                    foreach (Informa i in informa)
                    {
                        inf.Add(i);
                    }
                }
                var listapp = (from t in db.Applications
                               join t1 in db.Informa
                                   on t.AP_bian equals t1.Inf_shen
                               join t2 in (from a in db.BOMini
                                           join b in db.Formation
                                               on a.BOM2_liao equals b.FM_bai
                                           select new
                                           {
                                               a.BOM2_id,
                                               a.BOM2_mioa,
                                               b.FM_ms,
                                               a.BOM2_ST
                                           })
                                   on t1.BOM2_id equals t2.BOM2_id
                               join t3 in (from a in db.duty
                                           join b in db.Dengli
                                               on a.dl_id equals b.DE_ID
                                           select new
                                           {
                                               a.du_name,
                                               b.DE_nae
                                           })
                                   on t2.BOM2_ST equals t3.du_name
                               select new
                               {
                                   t.AP_bian,
                                   t.App_qishi,
                                   t.AP_ren,
                                   t.App_Kehu,
                                   t.APP_name,
                                   t.App_bu,
                                   t.App_JTime,
                                   t.App_fzr,
                                   t1.Inf_wu,
                                   t1.Inf_htime,
                                   t1.Inf_ftime,
                                   t1.Inf_gong,
                                   t1.Inf_id,
                                   t1.Inf_shen,
                                   t1.Inf_huiP,
                                   t1.BOM2_id,
                                   t1.Inf_JT,
                                   t1.Inf_sczq,
                                   t1.Inf_ewfy,
                                   t1.Inf_ybfy,
                                   t2.BOM2_mioa,
                                   t2.FM_ms,
                                   t2.BOM2_ST,
                                   t3.DE_nae
                               }).ToList();
                if (danhao != null && danhao != "")
                {
                    listapp = listapp.Where(x => x.AP_bian.Contains(danhao)).ToList();
                }
                if (BB_SQ_xmmc != null && BB_SQ_xmmc != "")
                {
                    listapp = listapp.Where(x => x.APP_name.Contains(BB_SQ_xmmc)).ToList();
                }
                if (BB_SQ_khmc != null && BB_SQ_khmc != "")
                {
                    listapp = listapp.Where(x => x.App_Kehu.Contains(BB_SQ_khmc)).ToList();
                }
                if (jssj != "")
                {
                    DateTime result = DateTime.Parse(jssj);
                    listapp = listapp.Where(x => DateTime.Parse(x.App_qishi) <= result).ToList();
                }
                if (qssj != null && qssj != "")
                {
                    DateTime result = DateTime.Parse(qssj);
                    listapp = listapp.Where(x => DateTime.Parse(x.App_qishi) >= result).ToList();
                }
                if (BB_SQ_gjz != null && BB_SQ_gjz != "")
                {
                    listapp = listapp.Where(x => x.AP_bian.Contains(BB_SQ_gjz) || x.APP_name.Contains(BB_SQ_gjz) || x.App_Kehu.Contains(BB_SQ_gjz) || x.Inf_wu.Contains(BB_SQ_gjz) || x.App_qishi.Contains(BB_SQ_gjz) || x.App_JTime.Contains(BB_SQ_gjz) || x.Inf_gong.Contains(BB_SQ_gjz)).ToList();
                }
                listapp = listapp.OrderByDescending(s => s.App_qishi).ToList();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listapp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                BOMini b = db.BOMini.Where(x => x.BOM2_id == id).FirstOrDefault();
                duty duty = db.duty.Where(x => x.du_name == b.BOM2_ST).FirstOrDefault();
                Dengli dengli = duty == null ? null : db.Dengli.Where(x => x.DE_ID == duty.dl_id).FirstOrDefault();
                string JNN = dengli == null ? "" : dengli.DE_nae;
                return Content(JNN);
            }
            else if (state == 3)
            {
                var JIN = db.BOMini.Where(x => x.BOM2_id == id).FirstOrDefault();
                string b = JIN == null ? "" : JIN.Formation == null ? "" : JIN.Formation.FM_ms;
                return Content(b);
            }
            return View();
        }

        public ActionResult Overview(string CG_INFSQD, string App_bian = "", string App_KHname = "", string App_xiang = "", int state = 0, int id = 0)
        {
            var listApp = find_APP_Information.find_app_information2(App_bian, App_KHname, App_xiang);

            ViewBag.listApp = listApp;
            if (state == 1)
            {
                var list = from s in listApp
                           select new { s.App_Kehu, s.App_qishi, s.App_JTime, s.AP_bian, s.APP_name, s.App_bu };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {

                var list = db.BOMini.Where(x => x.BOM2_bian == App_bian && x.BOM2_Lei == "外购").ToList();
                var fzr_id = db.duty.ToList();
                List<BOMini> list2 = new List<BOMini>();
                List<duty> temp = new List<duty>();
                foreach (BOMini b in list)
                {
                    var jeiji = b.BOM2_BQing;
                    int twwe = list.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                    if (twwe == 0)
                    {
                        list2.Add(b);
                    }

                }
                foreach (BOMini b in list2)
                {
                    foreach (duty d in fzr_id)
                    {
                        int index = 0;
                        foreach (duty d2 in temp)
                        {
                            if (d.dl_id == d2.dl_id)
                            {
                                index++;
                            }
                        }

                        if (b.BOM2_ST == d.du_name && index == 0)
                        {
                            temp.Add(d);
                        }
                    }
                }
                var temp4 = from a in temp
                            select new { a.dl_id, DE_nae = a.Dengli.DE_nae };


                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp4, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                string temp = "";
                int list = db.Informa.Where(x => x.Inf_shen == App_bian).Count();
                if (list > 0)
                {
                    int list2 = db.Informa.Where(x => x.Inf_shen == App_bian && x.Inf_huiP != null).Count();
                    temp = list2 + "/" + list + "供应商已回价";
                }
                else
                {
                    temp = "待询价";
                }
                return Content(temp);
            }
            else if (state == 4)
            {
                var list = db.BOMini.Where(x => x.BOM2_bian == App_bian).ToList();
                var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员

                var duty = new List<duty>();

                foreach (var d in list)
                {
                    var st = d.BOM2_ST;
                    st = st == "缓冲材" ? "泡沫（裁切/粘合）" : st;
                    var du = db.duty.Where(x => x.du_name == st).ToList();//人员
                    foreach (var item in du)
                    {
                        duty.Add(item);
                    }
                }
                var index = from a in duty
                            select new { a.dl_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 5)
            {
                var list = db.Informa.Where(x => x.Inf_shen == App_bian).ToList();
                var temp = from a in list
                           select new { a.Inf_id, a.Inf_shen, a.Inf_wu, a.Inf_liao, a.Inf_miao, a.Inf_nian, a.Inf_gong, a.Inf_huiP, a.Inf_zhuang, a.Inf_huiTime, a.Inf_JT };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 6)
            {
                var lid = db.BOMini.Where(x => x.BOM2_bian == App_bian && x.BOM2_Lei == "外购").ToList();
                List<BOMini> list2 = new List<BOMini>();
                var iindex = 0;
                string zhuang = "";
                foreach (BOMini b in lid)
                {
                    var jeiji = b.BOM2_BQing;
                    int twwe = lid.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                    if (twwe == 0)
                    {
                        list2.Add(b);
                    }

                }
                foreach (BOMini b2 in list2)
                {
                    if (b2.BOM2_sunhao != "已询价" && b2.BOM2_sunhao != "已核价")
                    {
                        iindex++;
                    }
                }
                if (iindex == 0)
                {
                    zhuang = "不添加";
                }
                else
                {
                    zhuang = "添加";
                }
                return Content(zhuang);
            }
            return View();
        }
        public ActionResult Price_main(string CG_INFSQD, string App_bian = "", string App_KHname = "", string App_xiang = "", int state = 0, int id = 0, int Inf_ids = 0)
        {
            var listApp = find_APP_Information.find_app_information(App_bian, App_KHname, App_xiang);
            ViewBag.listApp = listApp;
            if (state == 1)
            {
                for (int i = listApp.Count - 1; i >= 0; i--)
                {
                    var bian = listApp[i].AP_bian;
                    var JIN = db.shenpingliu.Where(x => x.FJINF == bian && x.FMIao == "核价审批").FirstOrDefault();
                    if (JIN != null)
                    {
                        if (JIN.Fzhuang == "审核中")
                        {
                            listApp.RemoveAt(i);
                        }
                        else if (JIN.Fzhuang == "驳回")
                        {
                            listApp[i].App_zhuang = "1";
                        }
                    }
                }
                var list = from s in listApp
                           select new { s.App_Kehu, s.App_qishi, s.App_JTime, s.AP_bian, s.APP_name, s.App_bu, s.App_zhuang };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                var listinf = FIWA.find_lnf(App_bian);
                var list = from s in listinf
                           select new { s.BOM2_id, s.Inf_id, s.Inf_shen, s.Inf_wu, s.Inf_liao, s.Inf_miao, s.Inf_nian, s.Inf_gong, s.Inf_huiTime, s.Inf_huiP, s.Inf_zhuang };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                string temp = "";
                int list = db.Informa.Where(x => x.Inf_shen == App_bian).Count();
                if (list > 0)
                {
                    int list2 = db.Informa.Where(x => x.Inf_shen == App_bian && x.Inf_huiP != null).Count();
                    temp = list2 + "/" + list + "供应商已回价";
                }
                else
                {
                    temp = "待询价";
                }
                return Json(temp);
            }
            else if (state == 4)
            {
                var listinf = FIWA.find_lnf2(App_bian);
                var list = from s in listinf
                           select new { s.BOM2_id, s.Inf_id, s.Inf_shen, s.Inf_wu, s.Inf_liao, s.Inf_miao, s.Inf_nian, s.Inf_gong, s.Inf_huiTime, s.Inf_huiP, s.Inf_zhuang, s.Inf_JT };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 5)
            {
                string temp = "加载";
                int list = db.shenpingliu.Where(x => x.FJINF == App_bian && x.FMIao == "核价审批" && x.Fzhuang == "审核中").Count();
                if (list > 0)
                {
                    temp = "不加载";
                }
                return Content(temp);
            }
            else if (state == 6)
            {
                int index = 0;
                var bom = db.BOMini.Where(x => x.BOM2_bian == App_bian && x.BOM2_Lei == "外购").ToList();

                var duby = db.duty.Where(x => x.dl_id == id).ToList();

                List<BOMini> bom2 = new List<BOMini>();
                List<BOMini> bom3 = new List<BOMini>();
                List<Informa> inf = new List<Informa>();

                foreach (BOMini b in bom)
                {
                    var jeiji = b.BOM2_BQing;
                    int twwe = bom.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                    if (twwe == 0)
                    {
                        bom2.Add(b);
                    }

                }

                foreach (BOMini b in bom2)
                {
                    foreach (duty d in duby)
                    {
                        if (b.BOM2_ST == d.du_name)
                        {
                            bom3.Add(b);
                        }
                    }
                }

                foreach (BOMini b2 in bom3)
                {
                    var listinf = db.Informa.Where(x => x.BOM2_id == b2.BOM2_id).ToList();
                    foreach (Informa inf2 in listinf)
                    {
                        if (inf2.Inf_zhuang != "已提交" && inf2.Inf_zhuang != "已确认")
                        {
                            index++;
                        }
                    }
                }
                if (index == 0)
                {
                    return Content("可以");
                }
                else
                {
                    return Content("不可以");
                }
            }
            else if (state == 7)
            {
                var temp1 = db.duty.Where(x => x.dl_id == id).ToList();
                var temp2 = db.Informa.Where(x => x.Inf_id == Inf_ids).FirstOrDefault();
                var bomini = db.BOMini.Where(x => x.BOM2_id == temp2.BOM2_id).FirstOrDefault();
                List<BOMini> bom = new List<BOMini>();
                List<Informa> bom3 = new List<Informa>();
                string zhuang = "不添加";
                foreach (duty d in temp1)
                {
                    if (d.du_name == bomini.BOM2_ST)
                    {
                        zhuang = "添加";
                    }
                }
                return Content(zhuang);
            }
            else if (state == 8)
            {
                var list = db.Informa.Where(x => x.Inf_shen == App_bian).ToList();
                list = list.GroupBy(d => new { d.Inf_gong }).Select(a => a.OrderBy(b => b.Inf_id).ElementAt(0)).ToList();
                var temp = from a in list
                           select new { a.Inf_gong };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 9)
            {
                var list = db.Informa.Where(x => x.Inf_shen == App_bian).ToList();
                if (App_xiang != null && App_xiang != "0" && App_xiang != "")
                {
                    list = list.Where(x => x.Inf_gong == App_xiang).ToList();
                }
                var temp = from a in list
                           select new { a.Inf_id, a.Inf_shen, a.Inf_wu, a.Inf_liao, a.Inf_miao, a.Inf_nian, a.Inf_gong, a.Inf_huiP, a.Inf_zhuang, a.Inf_huiTime, a.Inf_JT, a.Inf_sczq, a.Inf_ybfy, a.Inf_ewfy };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 10)
            {

                var list = db.BOMini.Where(x => x.BOM2_bian == App_bian).ToList();
                var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员

                var duty = new List<duty>();

                foreach (var d in list)
                {
                    var st = d.BOM2_ST;
                    st = st == "缓冲材" ? "泡沫（裁切/粘合）" : st;
                    var du = db.duty.Where(x => x.du_name == st).ToList();//人员
                    foreach (var item in du)
                    {
                        duty.Add(item);
                    }
                }
                var index = from a in duty
                            select new { a.dl_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 11)
            {
                int index = 0;
                string zhuang = "";
                Applications app = db.Applications.Where(x => x.AP_bian == App_bian).FirstOrDefault();
                if (app.App_bu == "4" || app.App_bu == "3")
                {
                    var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员
                    var listBOMw = db.BOMini.Where(x => x.BOM2_Lei == "外购" && x.BOM2_bian == App_bian).ToList();
                    List<BOMini> list = new List<BOMini>();
                    List<BOMini> list3 = new List<BOMini>();

                    foreach (BOMini b in listBOMw)
                    {
                        var jeiji = b.BOM2_BQing;
                        int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                        if (twwe == 0)
                        {
                            list.Add(b);
                        }
                    }
                    foreach (duty d in list2)
                    {
                        foreach (BOMini b in list)
                        {
                            if (d.du_name == b.BOM2_ST)
                            {
                                list3.Add(b);
                            }
                        }
                    }
                    if (list3.Count != 0)
                    {
                        foreach (BOMini b in list3)
                        {
                            if (b.BOM2_sunhao != "已询价")
                            {
                                index++;
                            }
                        }
                    }
                    else
                    {
                        index = 1;
                    }

                    if (index == 0)
                    {
                        zhuang = "添加";
                    }
                    else
                    {
                        zhuang = "不添加";
                    }
                }
                else if (app.App_bu == "5")
                {
                    var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员
                    var listBOMw = db.BOMini.Where(x => x.BOM2_Lei == "外购" && x.BOM2_bian == App_bian).ToList();
                    List<BOMini> list = new List<BOMini>();

                    foreach (BOMini b in listBOMw)
                    {
                        var jeiji = b.BOM2_BQing;
                        int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                        if (twwe == 0)
                        {
                            list.Add(b);
                        }
                    }

                    foreach (duty d in list2)
                    {
                        foreach (BOMini b in list)
                        {
                            if (d.du_name == b.BOM2_ST)
                            {
                                index++;
                            }
                        }
                    }
                    if (index != 0)
                    {
                        zhuang = "添加";
                    }
                    else
                    {
                        zhuang = "不添加";
                    }
                }
                return Content(zhuang);
            }
            else if (state == 12)
            {
                int index = 0;
                string zhuang = "";
                Applications app = db.Applications.Where(x => x.AP_bian == App_bian).FirstOrDefault();
                if (app.App_bu == "4" || app.App_bu == "3")
                {
                    var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员
                    var listBOMw = db.BOMini.Where(x => x.BOM2_Lei == "外购" && x.BOM2_bian == App_bian).ToList();
                    List<BOMini> list = new List<BOMini>();
                    List<BOMini> list3 = new List<BOMini>();

                    foreach (BOMini b in listBOMw)
                    {
                        var jeiji = b.BOM2_BQing;
                        int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                        if (twwe == 0)
                        {
                            list.Add(b);
                        }

                    }

                    foreach (duty d in list2)
                    {
                        foreach (BOMini b in list)
                        {
                            if (d.du_name == b.BOM2_ST)
                            {
                                list3.Add(b);
                            }
                        }
                    }

                    if (list3.Count != 0)
                    {
                        foreach (BOMini b in list3)
                        {
                            if (b.BOM2_sunhao != "已询价")
                            {
                                index++;
                            }
                        }
                    }
                    else
                    {
                        index = 0;
                    }

                    if (index != 0)
                    {
                        zhuang = "添加";
                    }
                    else
                    {
                        zhuang = "不添加";
                    }
                }
                return Content(zhuang);
            }
            else if (state == 13)
            {
                int index = 0;
                string zhuang = "";
                Applications app = db.Applications.Where(x => x.AP_bian == App_bian).FirstOrDefault();
                if (app.App_bu == "4" || app.App_bu == "3")
                {
                    var list2 = db.duty.Where(x => x.dl_id == id).ToList();//人员
                    var listBOMw = db.BOMini.Where(x => x.BOM2_bian == App_bian).ToList();
                    List<BOMini> list = new List<BOMini>();
                    List<BOMini> list3 = new List<BOMini>();

                    foreach (BOMini b in listBOMw)
                    {
                        var jeiji = b.BOM2_BQing;
                        int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                        if (twwe == 0)
                        {
                            list.Add(b);
                        }

                    }

                    foreach (duty d in list2)
                    {
                        foreach (BOMini b in list)
                        {
                            if (d.du_name == b.BOM2_ST)
                            {
                                list3.Add(b);
                            }
                        }
                    }

                    if (list3.Count != 0)
                    {
                        foreach (BOMini b in list3)
                        {
                            if (b.BOM2_GY != "完成")
                            {
                                index++;
                            }
                        }
                    }
                    else
                    {
                        index = 0;
                    }

                    if (index != 0)
                    {
                        zhuang = "添加";
                    }
                    else
                    {
                        zhuang = "不添加";
                    }
                }
                return Content(zhuang);
            }
            else if (state == 14)
            {
                var shen = db.shenpingliu.Where(s => s.FJINF == CG_INFSQD && s.FMIao == "核价审批" && s.Fzhuang == "驳回").FirstOrDefault();
                var shunkm = "";
                if (shen == null)
                {
                    shunkm = "";
                }
                else
                {
                    if (shen.FFZ == "驳回")
                    {
                        shunkm += shen.FFIZ;
                        shunkm += ":" + shen.Fliy;
                    }
                    else if (shen.FTZ == "驳回")
                    {
                        shunkm += shen.FFIZ;
                        shunkm += ":" + shen.Fliy;
                    }
                }
                return Content(shunkm);
            }
            return View();
        }

        public ActionResult Detailed(string APP_bian, string dasdnfj, string LH = null, string Button_te = null, int state = 0, string yonghu = "", string LiaoH = "", int id = 0)
        {
            var listGYS = GYS_LH.Find_GYS(LH);
            var listAgys = all_GYS.Find_ZHIding(Button_te, LiaoH);
            var listpr = all_GYS.find_GYS_bjxt(Button_te);
            var listAPP = find_APP_Information.find_app_information(APP_bian);
            var listBOM = BwAbian.find_BOMing(APP_bian);//修改自find_bom 删除leve0的情况
            var ListWL = FW.find_wl();
            ViewBag.PRO_kehu = listAPP;
            ViewBag.lsitPR_name = listAgys;
            ViewBag.efdm = listpr;
            List<Applications> applications = db.Applications.Where(s => s.AP_bian == APP_bian).ToList();
            List<BOMini> bOMinis = db.BOMini.Include(s => s.Formation).Where(s => s.BOM2_bian == APP_bian).OrderBy(s => s.BOM2_BQing).ToList();
            ViewBag.fgh = bOMinis;
            ViewBag.appji = applications;
            if (state == 0)
            {
                Session["appp_id"] = dasdnfj;
            }
            if (state == 2)
            {
                var list = from s in listpr
                           select new { s.PR_name };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 7)
            {
                var list = from s in listAgys
                           select new { s.PR_name };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);
                return Content(jsongStr, "application/json");
            }
            else if (listGYS != null && state == 5)
            {
                var result = from c in listGYS
                             join o in listAgys on c.PR_ID equals o.PR_ID
                             select new { CustomerName = c.PR_ID, OrderID = o.PR_name };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(result, settings);
                return Content(jsongStr, "application/json");

            }
            else if (state == 6)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listAPP, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 4)
            {
                string xmzt = db.Applications.Where(x => x.AP_bian == APP_bian).FirstOrDefault().App_lei;
                var listBOMw = db.BOMini.Where(x => x.BOM2_Lei == "外购" && x.BOM2_bian == APP_bian).ToList();
                List<BOMini> list2 = new List<BOMini>();

                foreach (BOMini b in listBOMw)
                {
                    var jeiji = b.BOM2_BQing;
                    var jeiji1 = b.BOM2_BQing + "-";
                    int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji1)).Count();
                    if (twwe == 0)
                    {
                        list2.Add(b);
                    }

                }

                var listnda = FN.find_nda();
                var result = from a in list2
                             join b in listnda on a.BOM2_wuLiu equals b.Nd_wuLiao into g
                             from x in g.DefaultIfEmpty()
                             select new { a.BOM2_id, a.BOM2_chen, a.BOM2_duan, a.BOM2_mioa, a.BOM2_Nian, Nd_jiage = x == null ? 0 : x.Nd_jiage, a.BOM2_Lei, a.BOM2_wuLiu, a.BOM2_liao, a.BOM2_ST, a.BOM2_Guige, a.BOM2_Sone };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(result, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                string number = FN.fand_BB(APP_bian);
                return Content(number);
            }
            else if (state == 9)
            {
                var shpi = FW.shenp(yonghu);
                var shikosKK = from a in shpi
                               select new { a.DE_nae, DE_shon = a.DE_shenfen == null ? "0" : (a.F_BuMen == null ? "0" : a.F_BuMen.FName) };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(shikosKK, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 1)
            {
                var list = db.duty.Where(x => x.dl_id == id).ToList();
                var temp = from a in list
                           select new { a.du_name };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 10)
            {
                var bkjsn = new BOMin();
                bkjsn.App_bian = APP_bian;
                bkjsn.Bom_chen = yonghu;
                bkjsn.BOM_liao = DateTime.Now.ToString("yyyy-MM-dd");
                bkjsn.BOM_guige = "0";//0:不足三家备注
                db.BOMin.Add(bkjsn);
                int sjin = db.SaveChanges();
                return Content("ss");
            }
            return View();
        }

        [HttpPost]
        public JsonResult AddCGXX(List<Informa> Informa)
        {
            if (Informa == null)
            {
                Informa = new List<Informa>();
            }
            int sun = 0;
            int JIN = 0;
            foreach (Informa a in Informa)
            {
                var sid = a.Inf_liao;
                var sjind = db.Formation.FirstOrDefault(s => s.FM_bai == sid);
                var sjinds = sjind == null ? "3000" : sjind.FM_Jieti == null ? "3000" : sjind.FM_Jieti.ToString();
                var KK = sjinds.Split(',');
                for (int i = 0; i < KK.Length; i++)
                {
                    var ID = KK[i];
                    int index = db.Informa.Where(x => x.BOM2_id == a.BOM2_id && x.Inf_gong == a.Inf_gong && x.Inf_JT == ID).Count();
                    if (index == 0)
                    {
                        a.Inf_JT = KK[i];
                        db.Informa.Add(a);
                        sun += db.SaveChanges();
                        JIN++;
                    }
                }
            }
            sun = JIN == sun ? 0 : 1;
            return Json(sun);
        }
        //询价审批
        [HttpPost]
        public ActionResult AddApplication(shenpingliu FJIDNS)
        {
            var shundn = FJIDNS.FJINF;
            var shinfko = db.shenpingliu.Where(s => s.FJINF == shundn && s.FMIao == "询价审批").FirstOrDefault();
            if (shinfko != null)
            {
                shinfko.FFirst = FJIDNS.FFirst;
                shinfko.FFZ = FJIDNS.FFZ;
                shinfko.FFS = FJIDNS.FFS;
                shinfko.FTwo = FJIDNS.FTwo;
                shinfko.FTZ = FJIDNS.FTZ;
                shinfko.FFive = FJIDNS.FFive;
                shinfko.FThree = FJIDNS.FThree;
                shinfko.FTTZ = FJIDNS.FTTZ;
                shinfko.FFIZ = FJIDNS.FFIZ;
                shinfko.FBzt = FJIDNS.FBzt;
                shinfko.FJINF = FJIDNS.FJINF;
                shinfko.FShenR = FJIDNS.FShenR;
                shinfko.FSHENP = FJIDNS.FSHENP;
                shinfko.FMIao = FJIDNS.FMIao;
                shinfko.FQsj = FJIDNS.FQsj;
                db.Entry(shinfko).State = EntityState.Modified;
            }
            else
            {
                db.shenpingliu.Add(FJIDNS);
            }
            int rowa = db.SaveChanges();
            string message = rowa > 0 ? "添加成功" : "添加失败";
            return Content(message, "text / plain");
        }
        [HttpPost]
        public ActionResult HeaApplication(shenpingliu FJIDNS)
        {

            var shundn = FJIDNS.FJINF;

            var shinfko = db.shenpingliu.Where(s => s.FJINF == shundn && s.FMIao == FJIDNS.FMIao).FirstOrDefault();

            if (shinfko != null)
            {
                shinfko.FFirst = FJIDNS.FFirst;
                shinfko.FFZ = FJIDNS.FFZ;
                shinfko.FFS = FJIDNS.FFS;
                shinfko.FTwo = FJIDNS.FTwo;
                shinfko.FTZ = FJIDNS.FTZ;
                shinfko.FFive = FJIDNS.FFive;
                shinfko.FThree = FJIDNS.FThree;
                shinfko.FTTZ = FJIDNS.FTTZ;
                shinfko.FFIZ = FJIDNS.FFIZ;
                shinfko.FBzt = FJIDNS.FBzt;
                shinfko.FJINF = FJIDNS.FJINF;
                shinfko.FShenR = FJIDNS.FShenR;
                shinfko.FSHENP = FJIDNS.FSHENP;
                shinfko.FMIao = FJIDNS.FMIao;
                shinfko.FQsj = DateTime.Now.ToString("yyyy/MM/dd");
                shinfko.Fzhuang = FJIDNS.Fzhuang;
                db.Entry(shinfko).State = EntityState.Modified;
            }
            else
            {
                db.shenpingliu.Add(FJIDNS);
            }
            int rowa = db.SaveChanges();
            string message = rowa > 0 ? "添加成功" : "添加失败";
            return Content(message, "text / plain");
        }
        public ActionResult Modify_GYS_HJ(Edit_Inf_jiad modle)
        {
            DateTime now = DateTime.Now;
            string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss");
            Informa inf = new Informa()
            {
                Inf_id = modle.Inf_id,
                Inf_zhuang = modle.Inf_zhuang,
                Inf_huiP = modle.Inf_huip,
                Inf_ewfy = modle.Inf_ewfy,
                Inf_sczq = modle.Inf_sczq,
                Inf_ybfy = modle.Inf_ybfy,
                Inf_htime = customFormat
            };

            int rows = MIH.ChangeInf_huip(inf);

            string message = rows > 0 ? "修改成功" : "修改失败";
            return Content(message, "text / plain");
        }

        public ActionResult Pricing(string APP_bian = null, int state = 0, int BOMid = -99, string ZLX = null, string WLXS = null, string ML = null, int MK = -99, string MB = null, string LH = null)
        {
            var inflist = FIWA.find_lnf(APP_bian);
            var ListZBxx = FZ.parices();
            ViewBag.ListZBxx = ListZBxx;
            ViewBag.ListINF = inflist;
            if (state == 1)
            {
                var ndalist = FN.find_nda();
                var bomlist = BwAbian.find_BOMed(APP_bian);


                var list = from s in bomlist
                           join x in ndalist on s.BOM2_wuLiu equals x.Nd_wuLiao into g
                           from y in g.DefaultIfEmpty()
                           select new { s.BOM2_id, s.BOM2_wuLiu, s.BOM2_liao, s.BOM2_chen, Nd_wuLiao = y == null ? "kong" : y.Nd_wuLiao };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");

            }
            else if (state == 2)
            {
                var ListINF_BOMid = FIWB.Find_INF(APP_bian, LH);
                var list = from s in ListINF_BOMid
                           select new { s.BOM2_id, s.Inf_id, s.Inf_gong, s.Inf_huiP, s.Inf_wu, s.Inf_liao };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                //var ListZB = FZ.parices(ZLX);
                //JsonSerializerSettings settings = new JsonSerializerSettings();
                //string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                //return Content(jsongStr, "application/json");
            }
            else if (state == 4)
            {
                var ListZB = FZ.parices_LX(WLXS);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 5)
            {
                //var ListZB = FZ.find_ZDJ(ML, MK);
                //JsonSerializerSettings settings = new JsonSerializerSettings();
                //string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                //return Content(jsongStr, "application/json");
            }
            else if (state == 6 && ML != null && MB != null)
            {
                //var ListZB = FZ.find_WDJ(ML, MB);
                //JsonSerializerSettings settings = new JsonSerializerSettings();
                //string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                //return Content(jsongStr, "application/json");
            }
            else if (state == 7)
            {
                var ListZB = FZ.find_JGF(ZLX);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                return Content(jsongStr, "application/json");
            }
            return View();
        }


        //[HttpPost]
        //public JsonResult AddNdardo(List<Ndardo> ndardos)
        //{
        //    int rows = 0;
        //    int index = 0;
        //    foreach (Ndardo a in ndardos)
        //    {
        //        var wl = FW.find_wlid(a.Nd_wuLiao);
        //        int num = AN.Add_ndardo(a.Nd_wuLiao);
        //        if (num > 0)
        //        {
        //            using (LDbaseEntities db = new LDbaseEntities())
        //            {

        //                if (ndardos == null)
        //                {
        //                    ndardos = new List<Ndardo>();
        //                }
        //                Ndardo nda = new Ndardo()
        //                {
        //                    Nd_Gong = a.Nd_Gong,
        //                    Nd_wuLiao = a.Nd_wuLiao,
        //                    Fo_id = a.Fo_id,
        //                    Nd_jiage=a.Nd_jiage,
        //                    Nd_danwei = a.Nd_danwei
        //                };
        //                rows += MN.ChangeNdardo(nda);
        //                Formation fm = new Formation()
        //                {
        //                    FM_name = a.Nd_wuLiao,
        //                    FM_ji = a.Nd_jiage.ToString(),
        //                };
        //                int row = MN.Changefm(fm);
        //            }
        //        }
        //        else
        //        {
        //            using (LDbaseEntities db = new LDbaseEntities())
        //            {
        //                if (ndardos == null)
        //                {
        //                    ndardos = new List<Ndardo>();
        //                }
        //                db.Ndardo.Add(a);
        //                index+= db.SaveChanges();
        //            }
        //        }

        //    }
        //    string all = "新增:" + index + "条," + "修改" + rows + "条";
        //    return Json(all);
        //}
        public ActionResult Modify_App_bu_g_x(string bian)
        {
            int index2 = 0;
            string temp = "";
            List<BOMini> BOM = db.BOMini.Where(x => x.BOM2_bian == bian && x.BOM2_Lei == "外购").ToList();
            List<BOMini> list2 = new List<BOMini>();
            foreach (BOMini b in BOM)
            {
                var jeiji = b.BOM2_BQing;
                int twwe = BOM.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                if (twwe == 0)
                {
                    list2.Add(b);
                }
            }
            foreach (BOMini b in list2)
            {
                if (b.BOM2_sunhao != "已询价")
                {
                    index2++;
                }
            }
            if (index2 == 0)
            {
                Applications applications = db.Applications.Where(x => x.AP_bian == bian).FirstOrDefault();
                if (applications.App_bu == "3")
                {
                    applications.App_bu = "4";
                    temp = "询价完成，等待工艺配置";
                }
                else if (applications.App_bu == "4")
                {
                    applications.App_bu = "5";
                    temp = "询价，工艺配置完成，请等待供应商回价";
                }
                db.SaveChanges();
            }
            return Content(temp);
        }
        public ActionResult Modify_BOM_WANC(int Hid)
        {
            try
            {
                int shu = 0;
                //判断物料是否已询价
                BOMini applications = db.BOMini.Where(x => x.BOM2_id == Hid).FirstOrDefault();
                var JKn = db.Informa.Where(s => s.BOM2_id == Hid).ToList();

                if (JKn.Count() > 0)
                {
                    applications.BOM2_sunhao = "已询价";
                    db.Entry(applications).State = EntityState.Modified;
                    shu = db.SaveChanges();
                }
                string meage = shu == 1 ? "1" : "0";
                return Content(meage);
            }
            catch (Exception ex)
            {
                return Content(ex.Message.ToString());
            }
            return View();
        }
        public ActionResult Modify_BOM_PD_hj(List<BOMini> id)
        {
            int shunf = 0;
            var ap_bain = "";
            var Wlia = "";//记录物料料号
            var HNF = 0;
            var bian = "";
            var BAn = "";
            //提交并修改BOM的字段，并检测外购的物料是否都询价
            foreach (var item in id)
            {
                bian = item.BOM2_bian;
                ap_bain = item.BOM2_bian;
                BOMini applications = db.BOMini.Where(x => x.BOM2_id == item.BOM2_id).FirstOrDefault();
                applications.BOM2_sunhao = "已核价";
                db.Entry(applications).State = EntityState.Modified;
                shunf += db.SaveChanges();
            }
            var BomimiS = db.BOMini.Where(s => s.BOM2_bian == bian && s.BOM2_Lei == "外购").ToList();
            foreach (var item in BomimiS)
            {
                if (item.BOM2_Lei == "外购")
                {
                    //是否询价
                    var Inf = db.Informa.Where(x => x.BOM2_id == item.BOM2_id && x.Inf_shen == item.BOM2_bian).ToList();
                    if (Inf.Count() <= 0)
                    {
                        HNF++;
                        Wlia += item.BOM2_liao.ToString() + ",";
                    }
                }
            }
            string temp = "";
            if (HNF == 0)
            {
                int index = 0;
                var appLO = db.Applications.Where(s => s.AP_bian == ap_bain).FirstOrDefault();
                List<BOMini> bom = db.BOMini.Where(x => x.BOM2_bian == ap_bain && x.BOM2_Lei == "外购" && x.BOM2_Zhuang == appLO.App_BanB).ToList();//外购的物料
                List<BOMini> bomZ = db.BOMini.Where(x => x.BOM2_bian == ap_bain && x.BOM2_Lei == "自制" && x.BOM2_Zhuang == appLO.App_BanB).ToList();//自制的物料
                List<BOMini> bomZ2 = db.BOMini.Where(x => x.BOM2_bian == ap_bain && x.BOM2_Zhuang == appLO.App_BanB).ToList();//所有的物料
                List<BOMini> list2 = new List<BOMini>();
                List<BOMini> sdw = new List<BOMini>();
                var JILU = 0;
                //查找leve0 de 数量及leve0 下是否存在子集Modify_BOM_WANC
                foreach (BOMini b in bomZ2)
                {
                    var jeiji = b.BOM2_BQing;
                    int twwe = bom.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                    if (twwe == 0)
                    {
                        sdw.Add(b);
                    }
                }
                if (sdw != null)
                {
                    foreach (var item in sdw)
                    {
                        //是否进行工艺
                        if (item.BOM2_GY == null && item.BOM2_Lei == "外购")
                        {
                            JILU++;
                        }
                    }
                }
                else
                {
                    foreach (var item in bomZ2)
                    {
                        //是否进行工艺
                        if (item.BOM2_GY == null)
                        {
                            JILU++;
                        }
                    }
                }
                foreach (BOMini b in bom)
                {
                    var jeiji = b.BOM2_BQing;
                    int twwe = bom.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                    if (twwe == 0)
                    {
                        list2.Add(b);
                    }
                }

                foreach (BOMini b in list2)
                {
                    if (b.BOM2_sunhao != "已核价")
                    {
                        index++;
                    }
                }

                if (index == 0)
                {
                    if (JILU == 0)
                    {
                        temp = "核价全部完成，请等待审核";
                    }
                    else
                    {
                        temp = "核价全部完成，自制的物料未进行工艺 ，请通知对应业务！";
                    }
                }
                else
                {
                    temp = "等待其他物流核价";
                }
            }
            else
            {
                temp = "" + Wlia + "未进行询价，请先去询价";
            }
            return Json(temp);
        }
        public ActionResult Modify_BOM_WANC_hj(int id)
        {
            BOMini applications = db.BOMini.Where(x => x.BOM2_id == id).FirstOrDefault();
            applications.BOM2_sunhao = "已核价";
            db.SaveChanges();

            int index = 0;
            string temp = "";

            List<BOMini> bom = db.BOMini.Where(x => x.BOM2_bian == applications.BOM2_bian && x.BOM2_Lei == "外购").ToList();
            List<BOMini> list2 = new List<BOMini>();
            foreach (BOMini b in bom)
            {
                var jeiji = b.BOM2_BQing;
                int twwe = bom.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji)).Count();
                if (twwe == 0)
                {
                    list2.Add(b);
                }
            }
            foreach (BOMini b in list2)
            {
                if (b.BOM2_sunhao != "已核价")
                {
                    index++;
                }
            }
            if (index == 0)
            {
                temp = "核价全部完成，请等待审核";
            }
            else
            {
                temp = "等待其他物流核价";
            }
            return Content(temp);
        }
        public ActionResult Modify_App_bu(Edit_App_bu modle)
        {
            Applications app = new Applications()
            {
                AP_bian = modle.Ap_bian,
                App_bu = modle.App_bu
            };

            int rows = MAB.ChangeApp_bu(app);

            string message = rows > 0 ? "修改成功" : "修改失败";
            return Content(message, "text / plain");

        }
        public JsonResult Modify_INF_zhuang3(List<Applications> modle)
        {
            int rows = 0;
            foreach (Applications i in modle)
            {
                if (modle == null)
                {
                    modle = new List<Applications>();
                }
                Applications inf = new Applications()
                {
                    AP_bian = i.AP_bian,
                    App_zhuang = i.App_zhuang,
                    APP_shr = i.APP_shr,
                    App_bu = i.App_bu,
                    APP_liy = i.APP_liy
                };
                rows += MAB.ChangeApp(inf);
                Designdraw designdraw = db.Designdraw.Where(x => x.D_Ton == i.AP_bian).FirstOrDefault();
                if (designdraw != null)
                {
                    designdraw.D_zhuang = "1";
                    db.SaveChanges();
                }
                if (i.App_zhuang == "3")
                {
                    var bO = db.BOMini.Where(x => x.BOM2_bian == i.AP_bian).ToList();
                    foreach (BOMini b in bO)
                    {
                        b.BOM2_GY = null;
                        db.SaveChanges();
                    }
                }
            }
            string message = "修改" + rows + "条";
            return Json(message);
        }
        public JsonResult Modify_INF_zhuang61(string names)
        {
            var BOMIJJ = db.BOMini.Where(s => s.BOM2_bian == names).ToList();
            var KML = db.BOMini.Where(s => s.BOM2_bian == names && s.BOM2_sunhao == "已核价").ToList();
            var message = BOMIJJ == KML ? "Y" : "N";
            return Json(message);
        }
        public JsonResult Modify_INF_zhuang6(List<Applications> modle)
        {
            int rows = 0;
            foreach (Applications i in modle)
            {
                if (modle == null)
                {
                    modle = new List<Applications>();
                }
                Applications inf = new Applications()
                {
                    AP_bian = i.AP_bian,
                    App_zhuang = i.App_zhuang,
                    APP_shr = i.APP_shr,
                    App_bu = i.App_bu,
                    APP_liy = i.APP_liy
                };
                rows += MAB.ChangeApp(inf);
            }
            string message = "修改" + rows + "条";
            return Json(message);
        }
        public JsonResult Modify_INF_zhuang4(List<Applications> modle)
        {
            int rows = 0;
            foreach (Applications i in modle)
            {
                if (modle == null)
                {
                    modle = new List<Applications>();
                }
                var zhuang = i.App_bu == "1" ? "保存" : "提交";
                Applications inf = new Applications()
                {
                    AP_bian = i.AP_bian,
                    App_zhuang = i.App_zhuang,
                    APP_shr = i.APP_shr,
                    App_bu = "1",
                    APP_liy = i.APP_liy,
                    App_bei = zhuang
                };
                rows += MAB.ChangeApp(inf);
                Designdraw designdraw = db.Designdraw.Where(x => x.D_Ton == i.AP_bian).FirstOrDefault();
                if (designdraw != null)
                {
                    designdraw.D_zhuang = i.App_bu;
                    db.SaveChanges();
                }
            }
            string message = rows.ToString();
            return Json(message);
        }
        public ActionResult Pricing_New(int state = 0, string APP_bian = null, int HJ_CP_cktj = 0)
        {
            var Koojdn = db.Applications.Where(s => s.AP_bian == APP_bian).ToList();
            ViewBag.PRO_kehu = Koojdn;
            Session["HJ_CP_appbian"] = APP_bian;
            if (state == 1)
            {
                string xmzt = db.Applications.Where(x => x.AP_bian == APP_bian).FirstOrDefault().App_lei;
                var listBOMw = db.BOMini.Where(x => x.BOM2_Lei == "外购" && x.BOM2_bian == APP_bian).ToList();
                List<BOMini> list2 = new List<BOMini>();

                foreach (BOMini b in listBOMw)
                {
                    var jeiji = b.BOM2_BQing;
                    var jeiji1 = b.BOM2_BQing + "-";
                    int twwe = listBOMw.Where(x => x.BOM2_BQing != jeiji && x.BOM2_BQing.Contains(jeiji1)).Count();
                    if (twwe == 0)
                    {
                        list2.Add(b);
                    }
                }

                var inflist = FIWA.find_lnf_witn_zhuang(APP_bian);
                var inflist2 = FIWA.find_lnf_witn_zhuang2(APP_bian);

                var index = from a in inflist
                            join b in inflist2 on a.BOM2_id equals b.BOM2_id into c
                            from d in c.DefaultIfEmpty()
                            select new { a.Inf_wu, Inf_huiP = d == null ? "0" : d.Inf_huiP, Inf_gong = d == null ? "" : d.Inf_gong, a.Inf_id };

                var temp = from e in index
                           join f in listBOMw on e.Inf_wu equals f.BOM2_wuLiu into g
                           from h in g.DefaultIfEmpty()
                           select new { h.BOM2_id, h.BOM2_wuLiu, h.BOM2_liao, h.BOM2_chen, h.BOM2_mioa, e.Inf_huiP, e.Inf_gong, h.BOM2_ST, e.Inf_id };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                //
                var bomlist = BwAbian.find_BOM(APP_bian);//返回询价物料行
                var listinf = FIWB.Find_INF(APP_bian);//查询已回价的物料信息
                var list = from s in listinf
                           join x in bomlist on s.Inf_id equals x.BOM2_id into g
                           from y in g.DefaultIfEmpty()
                           select new { Inf_huiP = s.Inf_huiP, Inf_gong = s.Inf_gong, Inf_id = s.Inf_id, Inf_wu = s.Inf_wu, Inf_liao = s.Inf_liao, s.BOMini.BOM2_mioa, s.BOMini.Fo_id };
                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore, // 处理循环引用  
                    Formatting = Formatting.Indented // 格式化输出，便于阅读  
                };
                string jsongStr = JsonConvert.SerializeObject(listinf, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                var listinf = FIWB.Find_INF(APP_bian, HJ_CP_cktj);
                var sjinfsns = from s in listinf
                               select new { Inf_huiP = s.Inf_huiP, Inf_gong = s.Inf_gong, Inf_id = s.Inf_id, Inf_wu = s.Inf_wu, Inf_liao = s.Inf_liao, s.BOMini.BOM2_mioa, s.BOMini.Fo_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(sjinfsns, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 4)
            {
                var listinf = FIWB.Find_INF_sx(APP_bian);
                var sjinfsns = from s in listinf
                               select new { Inf_huiP = s.Inf_huiP, Inf_gong = s.Inf_gong, Inf_id = s.Inf_id, Inf_wu = s.Inf_wu, Inf_liao = s.Inf_liao, s.BOMini.BOM2_mioa, s.BOMini.Fo_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(sjinfsns, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 5)
            {
                var listinf = FIWB.Find_INF_jx(APP_bian);
                var sjinfsns = from s in listinf
                               select new { Inf_huiP = s.Inf_huiP, Inf_gong = s.Inf_gong, Inf_id = s.Inf_id, Inf_wu = s.Inf_wu, Inf_liao = s.Inf_liao, s.BOMini.BOM2_mioa, s.BOMini.Fo_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(sjinfsns, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 6)
            {
                var list = db.Informa.Where(x => x.BOM2_id == HJ_CP_cktj).ToList();//查询指定物料询价的价格
                var shun = "";
                if (list.Count() > 0)
                {
                    //根据询价的价格生成datatable的表头
                    System.Data.DataTable dt = XUNTbale(HJ_CP_cktj, list);
                    //返回整体table
                    shun = ConvertToHtml(dt, HJ_CP_cktj);
                }
                else
                {
                    shun = "F";
                }
                return Content(shun);
            }
            else if (state == 7)
            {
                string cin = db.Formation.Where(x => x.FM_ID == HJ_CP_cktj).FirstOrDefault().FM_Jieti;
                return Content(cin);
            }
            else if (state == 8)
            {
                var temp = db.BOMini.Where(x => x.BOM2_bian == APP_bian).ToList();
                var index = from a in temp
                            select new { a.BOM2_wuLiu, a.BOM2_liao, a.BOM2_Nian, a.BOM2_chen, a.BOM2_Yong, a.BOM2_Sone, a.BOM2_ST, a.BOM2_Guige, a.BOM2_fenzi, a.BOM2_mioa, a.BOM2_id };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(index, settings);
                return Content(jsongStr, "application/json");

            }
            else if (state == 9)
            {
                string name = db.BOMini.Where(x => x.BOM2_id == HJ_CP_cktj).FirstOrDefault().BOM2_wuLiu;
                int id = db.ProcessRecord.Where(x => x.wl_name == name).FirstOrDefault().prr_id;

            }
            return View();
        }

        public System.Data.DataTable XUNTbale(int HJ_CP_cktj, List<Informa> list)
        {
            var jins = list.DistinctBy(s => s.Inf_gong).ToList();
            var dt = new System.Data.DataTable();
            dt.Columns.Add("阶梯价", typeof(int));
            dt.Columns.Add("理论价格", typeof(string));
            for (int i = 0; i < jins.Count; i++)
            {
                var sjnd = jins[i].Inf_gong;
                dt.Columns.Add(sjnd, typeof(string));
            }
            //确定行 物料的阶梯分解
            var BOM = db.BOMini.Find(HJ_CP_cktj).BOM2_liao;//料号
            var BOMGong = db.BOMini.Find(HJ_CP_cktj).BOM2_Gong;//保存的工艺记录
            var BOMMM = db.BOMini.Find(HJ_CP_cktj).BOM2_mioa;
            var fe = db.Formation.Where(gh => gh.FM_bai == BOM).FirstOrDefault();
            var OPGO = db.operational.Where(gh => gh.OP_ID == BOMGong).FirstOrDefault();//工艺价格的记录行
            var sjind = fe == null ? "" : fe.FM_Jieti;
            var OGOng = OPGO == null ? "" : OPGO.OP_jiehe == null ? "" : OPGO.OP_jiehe;
            var sjd = sjind.Split(',');
            //拆分价格   判断是否存在阶梯   无--3000
            var JietiList = OGOng.Split(';');//分解字段为 阶梯价格：阶梯    集合或空

            for (int i = 0; i < sjd.Length; i++)
            {
                var DTint = int.Parse(sjd[i]);
                var jiage = "";
                for (int k = 0; k < JietiList.Length; k++)
                {
                    var shuti = JietiList[k].Split(':');
                    if (shuti.Length == 1 || JietiList[k] == "")
                    {
                        jiage = BOMMM;
                    }
                    else if (shuti[1] == DTint.ToString())
                    {
                        jiage = shuti[0];
                        break;
                    }
                    else
                    {
                        jiage = BOMMM;
                    }
                }
                dt.Rows.Add(DTint, jiage);
            }

            //查询填充
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 2; j < dt.Columns.Count; j++)
                {
                    DataRow dataRow = dt.Rows[i];//第几行
                    DataColumn dc = dt.Columns[j];//第几列
                    var A = dataRow[0].ToString();//阶梯
                    var B = dc.ToString();//供应商
                    A = A == "" ? null : A;
                    var shund = list.Where(s => s.Inf_gong == B && s.Inf_JT == A).FirstOrDefault();
                    if (shund != null)
                    {
                        var jinn = shund.Inf_huiP;
                        var Infid = shund.Inf_id;
                        var shunf = Infid + ";" + jinn;
                        dataRow[j] = shunf;
                    }
                }
            }
            DataView dataView = dt.DefaultView;
            dataView.Sort = "阶梯价";
            var dtCopy = dt.Copy();
            dtCopy = dataView.ToTable();
            return dtCopy;
        }

        public JsonResult Modify_INF_zhuang(List<Informa> modle)
        {
            int rows = 0;
            //修改已选中的供应商
            foreach (var item in modle)
            {
                var I = item.Inf_id;
                var SAN = db.Informa.FirstOrDefault(s => s.Inf_id == I);
                var bian = SAN.Inf_shen;
                var wuliso = SAN.Inf_wu;
                var shund = db.Informa.Where(s => s.Inf_shen == bian && s.Inf_wu == wuliso).ToList();
                foreach (var s in shund)
                {
                    s.Inf_zhuang = "已提交";
                    db.Entry(s).State = EntityState.Modified;
                }
                db.SaveChanges();
            }

            foreach (Informa i in modle)
            {
                var list = db.Informa.Where(x => x.Inf_id == i.Inf_id).FirstOrDefault();
                list.Inf_zhuang = i.Inf_zhuang;
                rows += db.SaveChanges();
            }
            string message = "修改" + rows + "条";
            return Json(message);

        }

        [HttpPost]
        public JsonResult AddNdardo_CP(List<Ndardo> ndardos)
        {
            int rows = 0;
            int index = 0;
            foreach (Ndardo a in ndardos)
            {
                var wl = FW.find_wlid(a.Nd_wuLiao.ToString());
                int num = AN.Add_ndardo(a.Fo_id);///=0 wu >0you

                if (wl == -99)
                {
                    if (num > 0)
                    {
                        Ndardo nda = new Ndardo()
                        {
                            Nd_Gong = a.Nd_Gong,
                            Nd_wuLiao = a.Nd_wuLiao,
                            Fo_id = a.Fo_id,
                            Nd_jiage = a.Nd_jiage,
                            Nd_danwei = a.Nd_danwei,
                            Nd_shengxiao = a.Nd_shengxiao,
                            Nd_shixiao = a.Nd_shixiao,
                        };
                        rows += MN.ChangeNdardo(nda);
                    }
                    else
                    {
                        using (LDbaseEntities db = new LDbaseEntities())
                        {
                            if (ndardos == null)
                            {
                                ndardos = new List<Ndardo>();
                            }
                            db.Ndardo.Add(a);
                            index += db.SaveChanges();
                        }
                    }
                }
                else
                {
                    Formation fm = new Formation()
                    {
                        FM_name = a.Nd_wuLiao,
                        FM_price = a.Nd_jiage,
                    };
                    int row = MN.Changefm(fm);
                }
            }
            string all = "新增:" + index + "条," + "修改" + rows + "条";
            return Json(all);
        }
        [HttpPost]
        public JsonResult AddLanjilu_2(int paid)
        {
            int rows = 0;
            int index = 0;
            var format = FW.find_ro(paid);
            string currentTime = (DateTime.Now).ToString("yyyy-MM-dd");
            string Y = (DateTime.Now.Year).ToString();
            string M = (DateTime.Now.Month).ToString();
            using (LDbaseEntities db = new LDbaseEntities())
            {

                LanJiLu lan = new LanJiLu()
                {
                    JL_Name = format.PA_ZhongL,
                    JL_Time = currentTime,
                    JL_Fen = "原材料",
                    JL_Price = (format.PA_T).ToString(),
                    JL_Y = Y,
                    JL_M = M,
                    JL_Fen2 = format.mateial.MA_fne
                };
                db.LanJiLu.Add(lan);
                index += db.SaveChanges();
            }
            string all = "新增:" + index + "条," + "修改" + rows + "条";
            return Json(all);
        }
        [HttpPost]
        public JsonResult AddLanjilu(List<Formation> formations)
        {
            int rows = 0;
            int index = 0;
            foreach (Formation a in formations)
            {
                var format = FW.find_wl(a.FM_bai);
                string name = FW.find_maname((int)format.MA_ID);
                using (LDbaseEntities db = new LDbaseEntities())
                {
                    if (formations == null)
                    {
                        formations = new List<Formation>();
                    }

                    LanJiLu lan = new LanJiLu()
                    {
                        JL_Name = format.FM_name,
                        JL_Time = a.FM_name,
                        JL_Fen = name,
                        JL_Price = (format.FM_price).ToString()
                    };

                    db.LanJiLu.Add(lan);
                    index += db.SaveChanges();
                }

            }
            string all = "新增:" + index + "条," + "修改" + rows + "条";
            return Json(all);
        }
        [HttpPost]
        public JsonResult Modify_All_GYS_HJ(List<Informa> Informas)
        {
            DateTime now = DateTime.Now;
            string customFormat = now.ToString("yyyy-MM-dd");

            using (LDbaseEntities db = new LDbaseEntities())
            {
                int rows = 0;
                if (Informas == null)
                {
                    Informas = new List<Informa>();
                }
                foreach (Informa l in Informas)
                {
                    Informa inf = new Informa()
                    {
                        Inf_id = l.Inf_id,
                        Inf_zhuang = "已提交",
                        Inf_huiP = l.Inf_huiP,
                        Inf_ewfy = l.Inf_ewfy,
                        Inf_ybfy = l.Inf_ybfy,
                        Inf_sczq = l.Inf_sczq,
                        Inf_htime = customFormat
                    };
                    rows += MIH.ChangeInf_huip(inf);
                }
                return Json(rows);
            }

        }
        [HttpPost]
        public JsonResult PLsancInf(List<Informa> Informas)
        {
            using (LDbaseEntities LD = new LDbaseEntities())
            {
                int Sum = 0;
                foreach (Informa l in Informas)
                {
                    var id = l.Inf_id;
                    var shun = LD.Informa.Find(id);
                    LD.Informa.Remove(shun);
                    Sum += LD.SaveChanges();
                }
                string meager = "删除" + Sum + "条";
                return Json(meager);
            }

        }

        public ActionResult pricing_New_2(int state = 0, string APP_bian = null, int HJ_CP_cktj = 0, string ZLX = null, string WLXS = null, string ML = null, int MK = -99)
        {
            var ListZBxx = FP.find_lib();
            ViewBag.ListZBxx = ListZBxx;
            ViewBag.ListPP = FP.find_manufacturer();
            ViewBag.ListZL = FP.find_ZL_();
            ViewBag.ListPO = FP.find_projectprs();
            ViewBag.parice = FP.find_parices();
            if (state == 1)
            {
                var inflist = FIWA.find_lnf_witn_zhuang2();
                var listinf = FIWA.find_lnf_witn_zhuang3();
                var listfor = FP.Find_formations();
                var listma = FP.find_mateial();
                var list2 = from q in listfor
                            join w in listma on q.MA_ID equals w.MA_ID into e
                            from r in e.DefaultIfEmpty()
                            select new { r.MA_ID, r.MA_fne, q.FM_name };

                var list = from s in inflist
                           join x in listinf on s.Inf_id equals x.Inf_id into y
                           from z in y.DefaultIfEmpty()
                           join a in list2 on s.Inf_wu equals a.FM_name into b
                           from c in b.DefaultIfEmpty()
                           select new { s.Inf_id, s.Inf_wu, s.Inf_liao, Inf_gong = z == null ? "" : z.Inf_gong, Inf_huiP = z == null ? "0" : z.Inf_huiP, MA_fne = c == null ? "" : c.MA_fne };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");
            }
            if (state == 2)
            {
                var listinf = FIWB.Find_INF();
                var list = from s in listinf
                           select new { s.Inf_huiP, s.Inf_gong, s.Inf_id, s.Inf_wu, s.Inf_liao };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listinf, settings);

                return Content(jsongStr, "application/json");
            }
            if (state == 3)
            {
                var listinf = FIWB.Find_INF(HJ_CP_cktj);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listinf, settings);

                return Content(jsongStr, "application/json");
            }
            if (state == 4)
            {
                var listinf = FIWB.Find_INF_sx();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listinf, settings);

                return Content(jsongStr, "application/json");
            }
            if (state == 5)
            {
                var listinf = FIWB.Find_INF_jx();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listinf, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 6)
            {
                var ListZB = FW.find_wl_name(ZLX);
                var list = from s in ListZB
                           select new { FM_long = s == null ? 0 : s.FM_long, FM_gao = s == null ? 0 : s.FM_gao, FM_kuan = s == null ? 0 : s.FM_kuan };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 7)
            {
                var ListZB = FZ.parices_LX(WLXS);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                return Content(jsongStr, "application/json");
            }
            else if (state == 8)
            {
                //var ListZB = FZ.find_ZDJ(ML, MK);
                //JsonSerializerSettings settings = new JsonSerializerSettings();
                //string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                //return Content(jsongStr, "application/json");
            }
            else if (state == 9)
            {
                var ListZB = FZ.find_JGF(ZLX);
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(ListZB, settings);

                return Content(jsongStr, "application/json");
            }
            return View();
        }

        [HttpPost]
        public JsonResult AddCGXX_CL(List<Informa> Informa)
        {
            using (LDbaseEntities db = new LDbaseEntities())
            {
                if (Informa == null)
                {
                    Informa = new List<Informa>();
                }
                foreach (Informa b in Informa)
                {
                    int index = db.Informa.Where(x => x.Inf_gong == b.Inf_gong && x.Inf_shen == b.Inf_shen && x.Inf_wu == b.Inf_wu).Count();
                    if (index == 0)
                    {
                        db.Informa.Add(b);
                    }
                }
                int sun = db.SaveChanges();
                return Json(sun);
            }

        }

        //public ActionResult Material_rebate(int state=0)
        //{
        //    if (state == 1)
        //    {
        //        var listinf = FIWA.find_lnf_witn_zhuang();

        //        JsonSerializerSettings settings = new JsonSerializerSettings();
        //        string jsongStr = JsonConvert.SerializeObject(listinf, settings);
        //        return Content(jsongStr, "application/json");
        //    }
        //    return View();
        //}

        //public ActionResult Select_material(int state=0)
        //{
        //    if (state == 1)
        //    {
        //        var listwl = FW.find_wl();
        //        var listnda = FN.find_nda();
        //        var list = from s in listwl
        //                   join a in listnda on s.FM_name equals a.Nd_wuLiao into d
        //                   from f in d.DefaultIfEmpty()
        //                   select new { s.FM_name, s.FM_bai, Nd_Gong=f==null?"":f.Nd_Gong, Nd_jiage=f==null?0:f.Nd_jiage, Nd_danwei=f==null?"":f.Nd_danwei};

        //        JsonSerializerSettings settings = new JsonSerializerSettings();
        //        string jsongStr = JsonConvert.SerializeObject(list, settings);
        //        return Content(jsongStr, "application/json");
        //    }
        //    return View();
        //}

        public JsonResult Modify_Nda_zhuang(List<Formation> Ndardos)
        {
            using (LDbaseEntities db = new LDbaseEntities())
            {
                int rows = 0;
                if (Ndardos == null)
                {
                    Ndardos = new List<Formation>();
                }
                foreach (Formation n in Ndardos)
                {
                    Formation nda = new Formation()
                    {
                        FM_name = n.FM_name,
                        FM_kong = n.FM_kong
                    };
                    rows = MNS.ChangeInf_huip(nda);
                }
                return Json(rows);
            }

        }
        Find_P FP = new Find_P();
        /// <summary>
        /// 物料核价pricing_New_2页面
        /// 纸板相关
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult paperboard(int state = 0, string miaoshu = null, string lengxing = null, string pingpai = null, string zhonglei = null)
        {
            if (state == 1)
            {
                var listLX = FP.find_LengXing(miaoshu);
                var temporarily = from a in listLX
                                  select new { a.LB_Zlei };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                var listJaS = FP.find_Wastage_ProcessingCost(miaoshu);
                var temporarily = from a in listJaS
                                  select new { a.LB_jia, a.LB_shun, a.LB_Zlei };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 3)
            {
                var listpfkz = FP.find_PKZ(pingpai);
                var temporarily = from a in listpfkz
                                  select new { a.PA_ZhongL, a.PA_LengBie, a.PA_T, a.PA_KZ, a.PA_ID };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 4)
            {
                var listdj = FP.find_DJ(pingpai, zhonglei);
                var temporarily = from a in listdj
                                  select new { a.PA_T };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(listdj, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }
        /// <summary>
        /// 物料核价pricing_New_2页面
        /// 木制品相关
        /// </summary>
        /// <returns></returns>
        public ActionResult woodwork(int state = 0, string miaoshu = null)
        {
            if (state == 1)
            {
                var listJaS = FP.find_JGF(miaoshu);
                var temporarily = from a in listJaS
                                  select new { a.LB_jia };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 2)
            {
                var listPrice = FP.find_DJ(miaoshu);
                var temporarily = from a in listPrice
                                  select new { a.PA_T, a.PA_Pinpai };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }
        /// <summary>
        /// 物料核价pricing_New_2页面
        /// 塑料制品相关
        /// </summary>
        /// <returns></returns>
        public ActionResult Plastic_product(int state = 0, string miaoshu = null, int num = -99)
        {
            if (state == 1)
            {
                var listPOr = FP.find_projectprs(miaoshu);
                var temporarily = from a in listPOr
                                  select new { PA_LengBie = a.parice.PA_LengBie, PA_T = a.parice.PA_T, PR_dai = a.PR_dai, PA_ZhongL = a.parice.PA_ZhongL, };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 2)
            {
                var listPOr = FP.find_JGF(miaoshu);
                var temporarily = from a in listPOr
                                  select new { a.LB_jia };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 3)
            {
                var listPOr = FP.find_DJ(miaoshu);
                var temporarily = from a in listPOr
                                  select new { a.PA_T };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 4)
            {
                var listPOr = FP.find_Rou(miaoshu);
                var listlib = FP.find_lib();

                var temporarily = from a in listPOr
                                  join b in listlib on a.LB_ID equals b.LB_ID into c
                                  from d in c.DefaultIfEmpty()
                                  select new { d.LB_fne, d.LB_jia, a.LB_ID };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 5)
            {
                var listPOr = FP.find_lib(num);
                var temporarily = from a in listPOr
                                  select new { a.LB_fne, a.LB_jia };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 6)
            {
                var listPOr = FP.find_Rou_HC(miaoshu);
                var temporarily = from a in listPOr
                                  select new { a.RO_bei, a.RO_yao };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 7)
            {
                var list1 = FP.find_Rou_XS();
                var list2 = FP.find_lib();
                var temp = from a in list1
                           join b in list2 on a.LB_ID equals b.LB_ID into c
                           from d in c.DefaultIfEmpty()
                           select new { d.LB_fne, d.LB_shun, d.LB_jia };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 8)
            {
                var list = FP.find_projectprs_2(miaoshu);
                var temp = from a in list
                           select new { a.PR_name };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            if (state == 9)
            {
                var list = FP.find_lib_id(num);
                var temp = from a in list
                           select new { a.LB_fne, a.LB_jia, a.ST_ID };

                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temp, settings);
                return Content(jsongStr, "application/json");
            }
            if (state==10)
            {
                var listPOr = FP.find_projectprs(miaoshu);
                var temporarily = from a in listPOr
                                  select new { PA_LengBie = a.parice.PA_LengBie, PA_T = a.parice.PA_T, PR_dai = a.PR_dai, PA_ZhongL = a.parice.PA_ZhongL,KZ=a.parice.PA_KZ };
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(temporarily, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }
        /// <summary>
        /// 将理论价格修改进询价表
        /// </summary>
        /// <param name="inf"></param>
        /// <returns></returns>
        public JsonResult Modify_employee_information(Informa inf)
        {
            LDbaseEntities db = new LDbaseEntities();

            int rows = inf.Inf_id;
            var denamnr = db.Informa.FirstOrDefault(s => s.Inf_id == rows);
            denamnr.Inf_bei = inf.Inf_bei;
            db.Entry(denamnr).State = EntityState.Modified;
            int sun = db.SaveChanges();
            string index = "成功" + sun + "个物料价格";
            return Json(index);
        }
        public static string ConvertToHtml(System.Data.DataTable table, int AAA)
        {
            LDbaseEntities LD = new LDbaseEntities();
            var BOM = LD.BOMini.Find(AAA).BOM2_wuLiu;
            var html = new StringBuilder();

            // 添加表格开始标签  
            html.AppendLine("<table border='1' class='table tab' id='HSJI_KO'>");
            //添加标体
            html.AppendLine("<tr>");
            html.AppendFormat("<th colspan='5' class='HJ_CP_WLM'style='font-size:150%'>{0}</th>", BOM);
            html.AppendLine("</tr>");
            // 添加表头  
            html.AppendLine("<tr>");
            foreach (DataColumn column in table.Columns)
            {
                html.AppendFormat("<th>{0}</th>", column.ColumnName);
            }
            html.AppendLine("</tr>");
            var husn = "his";
            Random random = new Random();
            var inji = 0;
            // 添加表格数据  ss
            foreach (DataRow row in table.Rows)
            {
                inji++;
                int sji = random.Next(100, 1000);
                husn += sji;
                html.AppendLine("<tr>");
                var sun = 0;
                foreach (DataColumn column in table.Columns)
                {
                    sun++;
                    if (sun > 2)
                    {
                        var cb = row[1].ToString() == "" ? "0" : row[1].ToString();
                        var shuund = row[column].ToString();
                        var s = shuund.Split(';');
                        var lr = (double.Parse(s[s.Length - 1].ToString()) - double.Parse(cb)) / double.Parse(cb) * 100;
                        html.AppendFormat("<td style='text-align:left'><lable>{0}</lable><input type='radio' name='{1}'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style='display:block'>上升或下降比例：{3}%</span><span style='display:none' name='HJ_CP_i_x1'>{2}</span></td>", s[s.Length - 1], husn, s[0], Math.Round(lr, 3));
                    }
                    else
                    {
                        html.AppendFormat("<td style='text-align:left'>{0}</td>", row[column]);
                    }
                }
                html.AppendLine("</tr>");
            }

            // 添加表格结束标签  
            html.AppendLine("</table>");

            return html.ToString();
        }
        public ActionResult jisndf(int state, string JNDSKO = "")
        {
            if (state == 1)
            {
                //查询物料的值
                var shidh = db.Formation.FirstOrDefault(s => s.FM_name == JNDSKO);
                var KOND = shidh == null ? "3000" : shidh.FM_Jieti;
                var sjind = KOND.Split(',');
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(sjind, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }

        public static System.Data.DataTable ExcelToDatatable(string fileName, string sheetName, bool isFirstRowColumn)
        {
            System.Data.DataTable data = new System.Data.DataTable();
            int startRow = 0;
            int cellCount = 0;//列数
            int rowCount = 0;//行数
            try
            {
                using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    // 创建HSSFWorkbook或XSSFWorkbook对象，取决于你的Excel文件格式  
                    IWorkbook workbook;
                    if (System.IO.Path.GetExtension(fileName).Equals(".xls"))
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        workbook = new XSSFWorkbook(fs);
                    }

                    // 获取第一个工作表  
                    ISheet sheet = workbook.GetSheetAt(0);
                    if (sheet != null)
                    {
                        IRow firstRow = sheet.GetRow(0);
                        cellCount = firstRow.LastCellNum; //第一行最后一个cell的编号 即总的列数
                        if (isFirstRowColumn)//如果第一行是标题行
                        {
                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)//第一行列数循环
                            {
                                DataColumn column = new DataColumn(firstRow.GetCell(i).StringCellValue);//获取标题
                                data.Columns.Add(column);//添加列
                            }
                            startRow = sheet.FirstRowNum + 1;//1（即第二行，第一行0从开始）
                        }
                        else
                        {
                            startRow = sheet.FirstRowNum;//0
                        }
                        //最后一行的标号
                        rowCount = sheet.LastRowNum;
                        for (int i = startRow; i <= rowCount; ++i)//循环遍历所有行
                        {
                            IRow row = sheet.GetRow(i);//第几行
                            if (row != null)
                            {
                                //将excel表每一行的数据添加到datatable的行中
                                DataRow dataRow = data.NewRow();
                                for (int j = row.FirstCellNum; j < cellCount; ++j)
                                {
                                    if (row.GetCell(j) != null) //同理，没有数据的单元格都默认是null
                                    {
                                        dataRow[j] = row.GetCell(j).ToString();
                                    }
                                }
                                data.Rows.Add(dataRow);
                            }
                            else
                            {
                                continue; //没有数据的行默认是null;
                            }

                        }
                    }
                    return data;
                }
            }
            catch (Exception)
            {

                return data;
            }
        }

        public ActionResult ExcelFilePath(HttpPostedFileBase file)
        {
            var path = "";
            DateTime now = DateTime.Now;
            string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                //解决方案 先保存到服务器后使用
                if (file != null && file.ContentLength > 0)
                {
                    // 指定保存文件的路径（服务器上的实际路径）  
                    path = Server.MapPath("~/Files/");
                    // 确保目录存在  
                    Directory.CreateDirectory(path);

                    // 生成唯一的文件名（这里只是简单示例，你可能需要更复杂的唯一性逻辑）  
                    string fileName = file.FileName;//只保存一个文件 有文件就删除

                    // 保存文件到服务器  
                    string filePath = Path.Combine(path, fileName);
                    file.SaveAs(filePath);//真实路径
                    System.Data.DataTable table = ExcelToDatatable(filePath, "Sheet1", true);


                    //自定义一个datatable
                    var table1 = new System.Data.DataTable();
                    table1.Columns.Add("id", typeof(string));
                    table1.Columns.Add("bian", typeof(string));
                    table1.Columns.Add("wlm", typeof(string));
                    table1.Columns.Add("gg", typeof(string));
                    table1.Columns.Add("wlzl", typeof(string));
                    table1.Columns.Add("jtj", typeof(string));
                    table1.Columns.Add("nyl", typeof(string));
                    table1.Columns.Add("gys", typeof(string));
                    table1.Columns.Add("hfrq", typeof(string));
                    table1.Columns.Add("hj", typeof(string));
                    table1.Columns.Add("ewjg", typeof(string));
                    table1.Columns.Add("sczq", typeof(string));
                    table1.Columns.Add("tbfy", typeof(string));

                    int temp = 0;
                    //去除空值
                    foreach (DataRow row in table.Rows)
                    {

                        if (row["采购编号"].ToString() != "" || row["采购编号"].ToString() != null)
                        {
                            table1.Rows.Add(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]);
                        }
                    }
                    foreach (DataRow data in table1.Rows)
                    {
                        int id = int.Parse(data[0].ToString());
                        Informa informa = db.Informa.Where(x => x.Inf_id == id).FirstOrDefault();
                        informa.Inf_huiP = data[9].ToString();
                        informa.Inf_zhuang = "已提交";
                        informa.Inf_ewfy = data[10].ToString();
                        informa.Inf_sczq = data[11].ToString();
                        informa.Inf_ybfy = data[12].ToString();
                        informa.Inf_htime = customFormat;
                        temp += db.SaveChanges();
                    }
                    return Json("成功回价" + temp + "条物料");
                }
                else
                {
                    // 返回失败消息  
                    return Json(new { success = false, message = "没有文件被上传或者文件大小为0." });
                }
            }
            catch (Exception)
            {
                return Json(new { Success = false, Message = "系统错误！" });
            }

            return View();
        }

        public ActionResult DownloadExcel(string bian = null, int name = 0, int state = 0)
        {
            try
            {
                if (state == 1)
                {

                    var list = db.BOMini.Where(x => x.BOM2_bian == bian).ToList();
                    var fzr_id = db.duty.Where(x => x.dl_id == name).ToList();
                    List<BOMini> temp = new List<BOMini>();
                    List<Informa> inf = new List<Informa>();
                    int temp3 = 0;
                    foreach (BOMini b in list)
                    {
                        foreach (duty d in fzr_id)
                        {
                            if (b.BOM2_ST == d.du_name)
                            {
                                temp.Add(b);
                            }
                        }
                    }
                    foreach (BOMini b2 in temp)
                    {
                        var inf2 = db.Informa.Where(x => x.BOM2_id == b2.BOM2_id).ToList();
                        foreach (Informa inf3 in inf2)
                        {
                            inf.Add(inf3);
                        }
                    }

                    inf = inf.OrderBy(s => s.Inf_wu).ThenBy(s => s.Inf_gong).ThenBy(s => int.Parse(s.Inf_JT)).ToList();
                    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                    using (ExcelPackage excel = new ExcelPackage())
                    {
                        // 添加工作表
                        var worksheet = excel.Workbook.Worksheets.Add("Sheet1");
                        // 写入数据
                        worksheet.Cells[1, 1].Value = "采购编号";
                        worksheet.Cells[1, 2].Value = "申请单号";
                        worksheet.Cells[1, 3].Value = "物料信息";
                        worksheet.Cells[1, 4].Value = "规格";
                        worksheet.Cells[1, 5].Value = "物料种类";
                        worksheet.Cells[1, 6].Value = "最晚回复日期";
                        worksheet.Cells[1, 7].Value = "预计年用量";
                        worksheet.Cells[1, 8].Value = "供应商";
                        worksheet.Cells[1, 9].Value = "阶梯价";
                        worksheet.Cells[1, 10].Value = "回价价格";
                        worksheet.Cells[1, 11].Value = "模具费用";
                        worksheet.Cells[1, 12].Value = "生产周期";
                        worksheet.Cells[1, 13].Value = "印版费用";
                        int i = 2;
                        foreach (Informa des in inf)
                        {
                            int index = 1;
                            worksheet.Cells[i, index].Value = des.Inf_id;
                            index++;
                            worksheet.Cells[i, index].Value = des.Inf_shen;
                            index++;
                            worksheet.Cells[i, index].Value = des.Inf_wu;
                            index++;
                            worksheet.Cells[i, index].Value = des.BOMini.BOM2_Guige;
                            index++;
                            worksheet.Cells[i, index].Value = des.BOMini.BOM2_ST;
                            index++;
                            worksheet.Cells[i, index].Value = des.Inf_huiTime;
                            index++;
                            worksheet.Cells[i, index].Value = des.BOMini.BOM2_Nian;
                            index++;
                            worksheet.Cells[i, index].Value = des.Inf_gong;
                            index++;
                            worksheet.Cells[i, index].Value = des.Inf_JT;
                            index++;
                            worksheet.Cells[i, index].Value = null;
                            index++;
                            worksheet.Cells[i, index].Value = null;
                            index++;
                            worksheet.Cells[i, index].Value = null;
                            index++;
                            worksheet.Cells[i, index].Value = null;
                            i++;

                        }
                        worksheet.Cells["A:Z"].AutoFitColumns();
                        string path = "";
                        path = Server.MapPath("~/Files/");
                        string Name = name + "物料询价单.xlsx";
                        // 确保目录存在  
                        Directory.CreateDirectory(path);

                        // 生成唯一的文件名（这里只是简单示例，你可能需要更复杂的唯一性逻辑）  
                        string fileName = Path.GetFileNameWithoutExtension(Name) + "_" + Guid.NewGuid() + Path.GetExtension(Name);

                        // 保存文件到服务器  
                        string filePath = Path.Combine(path, fileName);
                        excel.SaveAs(filePath);
                        return Json(filePath);
                    }
                }
            }
            catch (Exception)
            {

            }

            return View();
        }

        public ActionResult DownloadExcel2(List<Applications> Designdraws, int state = 0, string excelName = null, int WT_CD = 0, string Employee_path = null)
        {
            if (state == 1)
            {
                //Employee_path = Employee_path.Replace(" ", string.Empty);
                //Employee_path = Employee_path.Substring(0, Employee_path.Length - 1);
                //Employee_path = Employee_path.Substring(1);
                // 创建Excel文件
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (ExcelPackage excel = new ExcelPackage())
                {
                    // 添加工作表
                    var worksheet = excel.Workbook.Worksheets.Add("Sheet1");
                    // 写入数据
                    worksheet.Cells[1, 1].Value = "申请单号";
                    worksheet.Cells[1, 2].Value = "业务员";
                    worksheet.Cells[1, 3].Value = "客户名称";
                    worksheet.Cells[1, 4].Value = "品名";
                    worksheet.Cells[1, 5].Value = "采购负责人";
                    worksheet.Cells[1, 6].Value = "客户要求阶梯价";
                    worksheet.Cells[1, 7].Value = "材质";
                    worksheet.Cells[1, 8].Value = "报价要求";
                    worksheet.Cells[1, 9].Value = "发起日期";
                    worksheet.Cells[1, 10].Value = "要求完成";
                    worksheet.Cells[1, 11].Value = "GSO提交日期";
                    worksheet.Cells[1, 12].Value = "报价日期";
                    worksheet.Cells[1, 13].Value = "供应商";
                    worksheet.Cells[1, 14].Value = "采购备注";
                    worksheet.Cells[1, 15].Value = "MOQ";
                    worksheet.Cells[1, 16].Value = "理论价格";
                    worksheet.Cells[1, 17].Value = "单价";
                    worksheet.Cells[1, 18].Value = "生产周期";
                    worksheet.Cells[1, 19].Value = "模具印版";
                    worksheet.Cells[1, 20].Value = "项目状态";
                    worksheet.Cells[1, 21].Value = "供应商不足三家原因";
                    worksheet.Cells[1, 22].Value = "供应商回价高于10%原因";
                    worksheet.Row(1).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
                    worksheet.Row(1).Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
                    int i = 2;
                    worksheet.Column(15).Width = 40;
                    foreach (Applications des in Designdraws)
                    {
                        int id = int.Parse(des.AP_bian);
                        Informa inf = db.Informa.Where(x => x.Inf_id == id).FirstOrDefault();
                        Applications app = db.Applications.Where(x => x.AP_bian == inf.Inf_shen).FirstOrDefault();
                        duty duty = db.duty.Where(x => x.du_name == inf.BOMini.BOM2_ST).FirstOrDefault();
                        Dengli dengli = db.Dengli.Where(x => x.DE_ID == duty.dl_id).FirstOrDefault();
                        Formation formation = db.Formation.Where(x => x.FM_bai == inf.BOMini.BOM2_liao).FirstOrDefault();
                        string zt = app.App_bu == "-99" ? "项目结束" : "项目执行中";
                        var husn = db.BOMin.Where(s => s.App_bian == inf.Inf_shen && (s.BOM_name == inf.BOM2_id.ToString() || s.BOM_name == null) && (s.BOM_guige == "0" || s.BOM_guige == null)).FirstOrDefault();//不足三家理由
                        var husn1 = db.BOMin.Where(s => s.App_bian == inf.Inf_shen && s.BOM_name == inf.BOM2_id.ToString() && s.BOM_guige == "2").FirstOrDefault();//低于10%理由
                        int index = 1;
                        worksheet.Cells[i, index].Value = inf.Inf_shen;
                        index++;
                        worksheet.Cells[i, index].Value = app.App_fzr;
                        index++;
                        worksheet.Cells[i, index].Value = app.App_Kehu;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_wu;
                        index++;
                        worksheet.Cells[i, index].Value = dengli.DE_nae;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_JT;
                        index++;
                        worksheet.Cells[i, index].Value = formation.FM_ms;
                        index++;
                        worksheet.Cells[i, index].Value = null;
                        index++;
                        worksheet.Cells[i, index].Value = app.App_qishi == null || app.App_qishi == "" ? DateTime.Parse("1980-01-01") : DateTime.Parse(app.App_qishi);
                        worksheet.Cells[i, index].Style.Numberformat.Format = "yyyy/mm/dd";
                        index++;
                        worksheet.Cells[i, index].Value = app.App_JTime == null || app.App_JTime == "" ? DateTime.Parse("1980-01-01") : DateTime.Parse(app.App_JTime);
                        worksheet.Cells[i, index].Style.Numberformat.Format = "yyyy/mm/dd";
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_ftime == null || inf.Inf_ftime == "" ? DateTime.Parse("1980-01-01") : DateTime.Parse(inf.Inf_ftime);
                        worksheet.Cells[i, index].Style.Numberformat.Format = "yyyy/mm/dd";
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_htime == null || inf.Inf_htime == "" ? DateTime.Parse("1980-01-01") : DateTime.Parse(inf.Inf_htime);
                        worksheet.Cells[i, index].Style.Numberformat.Format = "yyyy/mm/dd";
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_gong;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_bei;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_JT;
                        index++;
                        worksheet.Cells[i, index].Value = inf.BOMini.BOM2_mioa;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_huiP;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_sczq;
                        index++;
                        worksheet.Cells[i, index].Value = inf.Inf_ewfy;
                        index++;
                        worksheet.Cells[i, index].Value = zt;
                        index++;
                        worksheet.Cells[i, index].Value = husn == null ? "" : husn.Bom_chen;
                        index++;
                        worksheet.Cells[i, index].Value = husn1 == null ? "" : husn1.Bom_chen;

                        worksheet.Row(i).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
                        worksheet.Row(i).Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;

                        i++;
                    }
                    worksheet.Cells["A:Z"].AutoFitColumns();
                    string path = "";
                    path = Server.MapPath("~/Files/");
                    string Name = excelName + ".xlsx";
                    // 确保目录存在  
                    Directory.CreateDirectory(path);

                    // 生成唯一的文件名（这里只是简单示例，你可能需要更复杂的唯一性逻辑）  
                    string fileName = Path.GetFileNameWithoutExtension(Name) + "_" + Guid.NewGuid() + Path.GetExtension(Name);

                    // 保存文件到服务器  
                    string filePath = Path.Combine(path, fileName);
                    excel.SaveAs(filePath);
                    return Json(filePath);
                }

            }
            return View();
        }
        public JsonResult Modify_Nng(List<Informa> Ndardos)
        {
            using (LDbaseEntities db = new LDbaseEntities())
            {
                int rows = 0;
                if (Ndardos == null)
                {
                    Ndardos = new List<Informa>();
                }
                foreach (var n in Ndardos)
                {
                    var ide = n.Inf_id;
                    var GUU = n.Inf_gong;
                    var ids = db.Informa.Where(s => s.Inf_id == ide).FirstOrDefault();
                    var nda = new BOMin()
                    {
                        App_bian = ids.Inf_shen,
                        Bom_chen = n.Inf_zhuang,
                        BOM_name = ids.BOM2_id.ToString(),
                        BOM_liao = DateTime.Now.ToString("yyyy-MM-dd"),
                        BOM_guige = GUU
                    };
                    db.BOMin.Add(nda);
                    rows = db.SaveChanges();
                }
                return Json(rows);
            }
        }

        public ActionResult copy(List<BOMini> bom)
        {
            string bid = "";
            string temp = "";
            int sd = 0;
            int ibid = 0;
            int opid = 0;
            foreach (BOMini b in bom)
            {
                if (b.BOM2_Guige == "1")
                {
                    bid = b.BOM2_id.ToString();
                    ibid = b.BOM2_id;
                }
            }
            BOMini bOMini = db.BOMini.Where(x => x.BOM2_id == ibid).FirstOrDefault();
            if (bOMini != null)
            {
                opid = (int)bOMini.BOM2_Gong;
            }

            ProcessRecord pr = db.ProcessRecord.Where(x => x.wl_liao == bid).FirstOrDefault();
            if (pr != null)
            {
                int prid = pr.prr_id;
                foreach (BOMini b in bom)
                {
                    if (b.BOM2_Guige != "1")
                    {
                        operational soperational = db.operational.Where(x => x.OP_ID == opid).FirstOrDefault();
                        var UHFN = 0;
                        if (soperational != null)
                        {
                            operational op = new operational()
                            {
                                OP_jiehe = soperational.OP_jiehe,
                                OP_price = soperational.OP_price,
                                OP_YUN = soperational.OP_YUN,
                                OP_JIA = soperational.OP_JIA,
                                OP_CAi = soperational.OP_CAi,
                                OP_GUAN = soperational.OP_GUAN,
                                OP_LR = soperational.OP_LR
                            };
                            db.operational.Add(op);
                            db.SaveChanges();
                            UHFN = op.OP_ID;
                        }

                        string liao = b.BOM2_id.ToString();
                        int id = b.BOM2_id;
                        string name = b.BOM2_wuLiu;

                        BOMini bOMini1 = db.BOMini.Where(x => x.BOM2_id == id).FirstOrDefault();
                        if (bOMini1 != null)
                        {
                            bOMini1.BOM2_Gong = UHFN;
                            db.SaveChanges();
                        }

                        var pro = new ProcessRecord()
                        {
                            wl_name = name,
                            wl_liao = liao,
                            wl_c = pr.wl_c,
                            wl_k = pr.wl_k,
                            wl_g = pr.wl_g,
                            wl_fen = pr.wl_fen,
                            wu_fenLei = pr.wu_fenLei,
                            wl_leng = pr.wl_leng,
                            wl_gui = pr.wl_gui,
                            wl_h = pr.wl_h,
                            wl_ll = pr.wl_ll,
                            wl_m = pr.wl_m,
                            wl_mj = pr.wl_mj,
                            wl_sj = pr.wl_sj,
                            wl_xing = pr.wl_xing
                        };
                        db.ProcessRecord.Add(pro);
                        sd += db.SaveChanges();

                        ProcessRecord pr2 = db.ProcessRecord.Where(x => x.wl_liao == liao && x.wl_name == name).FirstOrDefault();
                        if (pr2 != null)
                        {
                            int prrid = pr2.prr_id;
                            List<ProcessFlow> pf = db.ProcessFlow.Where(x => x.prr_id == prid).ToList();
                            foreach (ProcessFlow flow in pf)
                            {
                                ProcessFlow pf2 = new ProcessFlow()
                                {
                                    gy_id = flow.gy_id,
                                    pfw_gg = flow.pfw_gg,
                                    pfw_jia = flow.pfw_jia,
                                    prr_id = prrid,
                                    CL_beizhu = flow.CL_beizhu,
                                    pfw_zc = flow.pfw_zc,
                                    pfw_Zhengfan = flow.pfw_Zhengfan
                                };
                                db.ProcessFlow.Add(pf2);
                                db.SaveChanges();
                            }
                            List<CraftMaterials> cm = db.CraftMaterials.Where(x => x.prr_id == prid).ToList();
                            foreach (CraftMaterials cra in cm)
                            {
                                CraftMaterials craft = new CraftMaterials()
                                {
                                    cl_id = cra.cl_id,
                                    prrw_c = cra.prrw_c,
                                    prrw_k = cra.prrw_k,
                                    prrw_g = cra.prrw_g,
                                    prrw_guige = cra.prrw_guige,
                                    prrw_jia = cra.prrw_jia,
                                    prrw_sh = cra.prrw_sh,
                                    prrw_zc = cra.prrw_zc,
                                    prrw_shu = cra.prrw_shu,
                                    prr_id = prrid,
                                };

                                db.CraftMaterials.Add(craft);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                temp = "成功同步" + sd + "条物料";
            }
            else
            {
                temp = "未选择母项或母项未配置工艺";
            }
            return Json(temp);
        }
        public ActionResult dle(List<BOMini> bom)
        {
            int ss = 0;
            foreach (BOMini b in bom)
            {
                if (b.BOM2_Guige != "1")
                {
                    string liao = b.BOM2_id.ToString();
                    int id = b.BOM2_id;
                    string name = b.BOM2_wuLiu;
                    ProcessRecord pr2 = db.ProcessRecord.Where(x => x.wl_liao == liao && x.wl_name == name).FirstOrDefault();
                    BOMini bOMini = db.BOMini.Where(x => x.BOM2_id == id).FirstOrDefault();
                    if (bOMini != null)
                    {
                        if (bOMini.BOM2_Gong != null)
                        {
                            int oid = (int)bOMini.BOM2_Gong;
                            operational opp = db.operational.Where(x => x.OP_ID == oid).FirstOrDefault();
                            if (opp != null)
                            {
                                db.operational.Remove(opp);
                                db.SaveChanges();
                            }
                        }

                    }
                    if (pr2 != null)
                    {
                        int prrid = pr2.prr_id;
                        List<ProcessFlow> pf = db.ProcessFlow.Where(x => x.prr_id == prrid).ToList();
                        List<CraftMaterials> cm = db.CraftMaterials.Where(x => x.prr_id == prrid).ToList();
                        foreach (ProcessFlow p in pf)
                        {
                            db.ProcessFlow.Remove(p);
                        }
                        foreach (CraftMaterials c in cm)
                        {
                            db.CraftMaterials.Remove(c);
                        }

                        db.ProcessRecord.Remove(pr2);
                        ss += db.SaveChanges();
                    }
                }
            }
            return Json("成功删除" + ss + "条");
        }
    }
}
